package com.xxl.job;

import net.ifok.image.image4j.codec.ico.ICOEncoder;

import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import javax.imageio.IIOImage;
import javax.imageio.ImageIO;
import javax.imageio.ImageWriteParam;
import javax.imageio.ImageWriter;
import javax.imageio.plugins.jpeg.JPEGImageWriteParam;
import javax.imageio.stream.ImageOutputStream;

public class IcoGenerator {

    public static void main(String[] args) throws IOException {
        FileInputStream in = new FileInputStream("D:\\tool\\code\\fresh\\fruit-stacks-fresh\\fresh-inn-web\\static\\images\\3.png");
        BufferedImage bufferedImage = ImageIO.read(in);
        if(bufferedImage == null){
            System.out.println("File not find");
            return;
        }
        List<BufferedImage> icons = new ArrayList<>();
        icons.add(getScaledInstance(bufferedImage, 16, 16));
        icons.add(getScaledInstance(bufferedImage, 32, 32));
        icons.add(getScaledInstance(bufferedImage, 64, 64));
        icons.add(getScaledInstance(bufferedImage, 128, 128));
        BufferedImage icon = getScaledInstance(bufferedImage, 128, 128);
        ICOEncoder.write(icon,new File("D:\\tool\\code\\fresh\\fruit-stacks-fresh\\fresh-inn-web\\static\\images\\favicon.ico"));
    }

    public static BufferedImage getScaledInstance(BufferedImage bufferedImage, int toWidth, int toHeight){
        Image scaledInstance = bufferedImage.getScaledInstance(toWidth, toHeight, 0);
        BufferedImage newBufferedImage = new BufferedImage(toWidth,
                toHeight, BufferedImage.TYPE_INT_RGB);
        newBufferedImage.createGraphics().drawImage(scaledInstance, 0, 0, Color.WHITE, null);
        return newBufferedImage;
    }
}